package edu.ycxy.poetry.controller;


import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.ChaoDai;
import edu.ycxy.poetry.service.IChaoDaiService;
import edu.ycxy.poetry.vo.AddDynastyVo;
import edu.ycxy.poetry.vo.UpdateDynastyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@RestController
@RequestMapping("/dynasty")
@Slf4j
public class ChaoDaiController {
    @Autowired
    private IChaoDaiService chaoDaiService;

    //根据朝代名查询朝代的详细信息,${name}-name占位符,获取前端的数据
    @RequestMapping("/{dynastyName}")
    public ChaoDai findByDynastyName(@PathVariable("dynastyName") String dynastyName){
        ChaoDai chaoDai = chaoDaiService.findDynastyByName(dynastyName);
        log.debug("查询的内容是:"+chaoDai);
        return chaoDai;
    }

    //查询朝代的所有名字
    @RequestMapping("/findAllDynastyName")
    public List<ChaoDai> findDynastyName(){
        return chaoDaiService.findDynastyName();
    }

    //朝代更新或添加
    @RequestMapping("/addNewDynasty")
    public String addDynasty(@RequestBody AddDynastyVo addDynastyVo){
        log.debug("获取的信息: " + addDynastyVo);
        String s = chaoDaiService.addDynasty(addDynastyVo);
        if (s.equals("朝代更新")){
            return "1";
        }else {
            return "2";
        }
    }

    /**
     * 根据朝代名进行模糊查询
     */
    @RequestMapping("/like/{name}")
    public PageInfo<ChaoDai> findListByNameToLike(@PathVariable("name") String name,Integer pageNum){
        if(pageNum == null){
            pageNum = 1;
        }
        PageInfo<ChaoDai> listByNameToLike = chaoDaiService.findListByNameToLike(name, pageNum, 5);
        return listByNameToLike;
    }

    // 根据id删除朝代信息
    @RequestMapping("/delete/{id}")
    public void deleteDynastyById(@PathVariable("id") Integer id){
        chaoDaiService.deleteDynastyById(id);
    }

    //根据id查询朝代信息
    @RequestMapping("/findOne/{id}")
    public UpdateDynastyVo findOneById(@PathVariable("id") Integer id){
        return chaoDaiService.findOneById(id);
    }

    // 更新朝代信息
    @RequestMapping("/update")
    public Integer updateDynasty(@RequestBody UpdateDynastyVo updateDynastyVo){
        Integer integer = chaoDaiService.updateDynasty(updateDynastyVo);
        if (integer == 1){
            return integer;
        }else {
            return null;
        }
    }
}
